package com.xt.partjobback.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xt.partjobback.dto.CollectDto;
import com.xt.partjobback.dto.JobDto;
import com.xt.partjobback.entity.Collect;
import com.xt.partjobback.entity.Job;
import com.xt.partjobback.entity.Message;
import com.xt.partjobback.mapper.CollectMapper;
import com.xt.partjobback.service.ClassIfyService;
import com.xt.partjobback.service.CollectService;
import com.xt.partjobback.service.JobService;
import com.xt.partjobback.utils.TokenUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author leotong
 */
@Service
@AllArgsConstructor
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    private CollectMapper collectMapper;

    private JobService jobService;

    private ClassIfyService classIfyService;

    @Override
    public Page<CollectDto> getCollectByUid(Collect collect) {
        Page<Collect> page=new Page<>(collect.pageNum==null?1: collect.getPageNum(),collect.pageSize==null?5: collect.getPageSize());
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<Collect>().eq("uid", TokenUtil.tokenExtractId(collect.getToken()));
        Page<Collect> collectPage = collectMapper.selectPage(page, queryWrapper);
        Page<CollectDto> result=new Page<>();
        result.setTotal(collectPage.getTotal());
        result.setRecords(new ArrayList<>());
        for (Collect record : collectPage.getRecords()) {
            CollectDto collectDto=new CollectDto();
            BeanUtils.copyProperties(record,collectDto);
            JobDto jobDto=new JobDto();
            Job job=jobService.getById(record.getJid());
            BeanUtils.copyProperties(job,jobDto);
            jobDto.setUseCollect(false);
            jobDto.setClassify(classIfyService.getById(job.getCid()));
            collectDto.setJobDto(jobDto);
            result.getRecords().add(collectDto);
        }
        return result;
    }
}
